<?php

class FutureDmsController extends AppController {
	public $helpers = array('Html', 'Form');
	public $components = array('Session');

	var $uses = array('FutureDm', 'User');

	public function test(){
		$this->delete('1');
	}

	/**
	 * Given a DM id, this function either deletes the specified id or notifies the user that such a 
	 * tweet is non-existent, unless the user lacks sufficient privilleges, in which case he is notified
	 * that he is not authorized to carry out such an action.
	 * @param  int $id DM id.
	 * @return void     A flash is shown.
	 * @author KareemYousry
	 */
	public function delete($id) {
		$accID = $this->Auth->User('accID');
		$userID = $this->Auth->User('userID');

		if($this->User->getPermissionLevel($accID, $userID) == 3 OR $this->User->isAdmin($userID))
		{
			$result = $this->FutureDm->remove($id);

			switch ($result) {
				case '0':
					$this->Session->setFlash('DM has been removed successfully');
					$this->redirect(array('action' => 'index'));
					break;
				
				case '1':
					$this->Session->setFlash('The specified DM does not exist');
					$this->redirect(array('action' => 'index'));
					break;
			}
		} else {
			$this->Session->setFlash('You are not authorized to delete this DM');
			$this->redirect(array('action' => 'index'));
		}
	}

	/**
	 * Given a DM id, this function either approves, disproves this DM or notifies the user that such
	 * a DM is non-existent. If the User lacks sufficient privilleges, he is notifed that he may not 
	 * carry out such an action.
	 * @param  int $id DM id.
	 * @return void     This function sets a flash to be displayed.
	 * @author KareemYousry
	 */
	public function approve($id) {
		$accID = $this->Auth->User('accID');
		$userID = $this->Auth->User('userID');

		if($this->User->getPermissionLevel($accID, $userID) == 3 OR $this->User->isAdmin($userID)) {
			$result = $this->FutureDm->approve($id);

			switch ($result) {
				case '0':
					$this->Session->setFlash('DM has been approved successfully');
					$this->redirect(array('action' => 'index'));
					break;

				case '1':
					$this->Session->setFlash('DM has been disproved succesfully');
					$this->redirect(array('action' => 'index'));
					break;

				case '2':
					$this->Session->setFlash('The specified DM does not exist');
					$this->redirect(array('action' => 'index'));
					break;
			}
		} else {
			$this->Session->setFlash('You are not authorized to approve this DM');
			$this->redirect(array('action' => 'index'));
		}
	} 

		/*
 * Description of "viewfuturedms"
 *  
 *
 *This function is for any user to view his direcmessages on a certain account (the tweets that haven't been sent).
 *
 * @author "Mark Milad"
 */
	public function index1() {
		$userID = $this->Auth->User('userID');
		$accID = $this->Auth->User('accID');
		$result = $this->FutureDm->displayDms($userID, $accID);
		$this->set('futureDms', $result);
	}

	/**
	 * This function provides a preview of the available FutureDms as per account. It also provides
	 * the view with a priv variable, upon which the view should decide whether or not the approve/
	 * delete links are viewed to the user accessing the page.
	 * @author Kareem Yousry.
	 */
	public function index() {
	$accID = $this->Auth->User('accID');
	$userID = $this->Auth->User('userID');
	$result = $this->FutureDm->retrieveDMs($accID);

	if($this->User->getPermissionLevel($accID, $userID) >= 3 OR $this->User->isAdmin($userID) == 1)
	{
		$this->set('priv', 1);
	} else {
		$this->set('priv', -1);
	}

	$this->set('futureDms', $result);
	
	}
/*
 * Description of "viewfuturedms"
 *  
 *
 *This function is for any user to view his direcmessages on a certain account (the tweets that haven't been sent).
 *
 * @author "Mark Milad"
 */
        public function viewfuturedms() {

                $userID = $this->Auth->User('userID');
                $accID = $this->Auth->User('accID');
                if($this->User->getPermissionLevel($accID, $userID) ==1){
                $this->set('futureDms', $this->FutureDm->find('all', array('conditions' => array("FutureDm.userID" => $userID, "FutureDm.accID" => $accID))));
                
            }
            else {
            	$this->Redirect('/FutureDms');
            }
        }


	/**
     * Description of function "deleteJEDm"
     *The function takes the Dm ID deletes it from the database, if it still exists
     * @param string $dmID
     * 
     *
     * 
     * 
     * @author "Mark Milad"
     */
	public function deleteJEDm($dmID){
		if($this->FutureDm->JEdeleteDm($dmID) == true)
      {
			   $this->Session->setFlash('Successfully deleted');
			   $this->redirect(array('action'=>'viewfuturedms'));
               
      }
      else {
        $this->redirect(array('action'=>'viewfuturedms'));
      }
	}

}